const match = amisRequire('path-to-regexp').match;

const customResponseAdaptor = (api, response, query, request) => {
    // console.log('custom response');
    response.status = response.code !== undefined ? response.code : 0;
    response.msg = response.message !== undefined ? response.message : '';
    delete response.code;
    delete response.message;
    if (response.status === 401) {
        // token失效
        // location.href = '/b/site/view.html';
        location.reload();
        return response;
    } else {
        return response;
    }
}

const normalizeLink = (to, location = window.location) => {
    to = to || '';

    if (to && to[0] === '#') {
        to = location.pathname + location.search + to;
    } else if (to && to[0] === '?') {
        to = location.pathname + to;
    }

    const idx = to.indexOf('?');
    const idx2 = to.indexOf('#');
    let pathname = ~idx
        ? to.substring(0, idx)
        : ~idx2
            ? to.substring(0, idx2)
            : to;
    let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
    let hash = ~idx2 ? to.substring(idx2) : location.hash;

    if (!pathname) {
        pathname = location.pathname;
    } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
        let relativeBase = location.pathname;
        const paths = relativeBase.split('/');
        paths.pop();
        let m;
        while ((m = /^\.\.?\//.exec(pathname))) {
            if (m[0] === '../') {
                paths.pop();
            }
            pathname = pathname.substring(m[0].length);
        }
        pathname = paths.concat(pathname).join('/');
    }

    return pathname + search + hash;
}

const jumpTo = to => {
    if (to.slice(0,3) === '/b/' || to.slice(0,6) === '/chat/') {
        // 后端路由直接跳转
        if ('/chat/index.html' === to.slice(0,16)) {
            window.open(to,'_blank');
        } else {
            location.href = to;
        }
    } else {
        location.hash = to;
        window.history.pushState('', document.title, location.pathname + location.hash);
    }
};

const updateLocation = (to, replace) => {
    if (to === 'goBack') {
        return window.history.back();
    }

    if (replace && window.history.replaceState) {
        window.history.replaceState(
            '',
            document.title,
            normalizeLink(to)
        );
        return;
    }

    window.history.pushState('', document.title, normalizeLink(to));
};

const watchRouteChange = (fn) => {
    window.addEventListener("hashchange", fn);
    return () => {
        window.removeEventListener("hashchange", fn);
    };
};

const isCurrentUrl = (to, ctx) => {
    if (!to) {
        return false;
    }
    const pathname = location.hash ? location.hash.substring(1) : '/';
    const link = normalizeLink(to, {
        ...location,
        pathname,
        hash: ''
    });

    if (!~link.indexOf('http') && ~link.indexOf(':')) {
        return match(link, {
            decode: decodeURIComponent,
            strict: (ctx && ctx.strict) ? ctx.strict : true
        })(pathname);
    }

    return pathname === encodeURI(link);
};